package com.mobisystems.youpark.settings;

import java.util.HashMap;
import java.util.Map;

import com.mobisystems.youpark.R;
import com.mobisystems.youpark.SettingsActivity;
import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.model.BankAccount;
import com.stripe.model.Recipient;
import com.stripe.model.Token;

import android.app.Activity;
import android.app.ActionBar;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class BankAccountActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bank_account);
		
		//using custom action bar
		ActionBar customActionBar = getActionBar();
		customActionBar.setDisplayShowHomeEnabled(false);
		customActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.custom_actionbar, null);
        TextView titleTextView = (TextView) view.findViewById(R.id.title_text);
        titleTextView.setText("Bank Account Information");
        view.findViewById(R.id.button_settings).setVisibility(View.INVISIBLE);
        view.findViewById(R.id.button_switch_modes).setVisibility(View.GONE);
		ImageButton backButton = (ImageButton) view.findViewById(R.id.button_back);
		backButton.setVisibility(View.VISIBLE);
		backButton.setBackgroundResource(R.drawable.back_button);
		backButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				onBackPressed();
			}
		});
        customActionBar.setCustomView(view);
        customActionBar.setDisplayShowCustomEnabled(true);
        
        
        // OnClick listener for SAVE button
        Button saveAccountButton = (Button)findViewById(R.id.save_account_button);
        saveAccountButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				EditText et1 = (EditText)findViewById(R.id.account_name_textbox);
				EditText et2 = (EditText)findViewById(R.id.account_number_textbox);
				EditText et3 = (EditText)findViewById(R.id.routing_number_textbox);
				String name = et1.getText().toString();
				String account_number = et2.getText().toString();
				String routing_number = et3.getText().toString();
				System.out.println("captured user inputs");
				
				
				new ProcessBankAccount().execute(name, account_number, routing_number);
				
				Intent settings = new Intent(BankAccountActivity.this, SettingsActivity.class);
				startActivity(settings);
				finish();
					
			}
		});  
        
        
        }
	
		
		private class ProcessBankAccount extends AsyncTask<String, Void, String>{
			
			@Override
			protected String doInBackground(String... params) {
				
				Stripe.apiKey = "sk_test_waGgPKE1zA91Nru33emcGIUL";	
				ParseUser currentUser = ParseUser.getCurrentUser();	
				
				String name = params[0];
				String account = params[1];
				String routing = params[2];
				
				try {
					
					Map<String, Object> bank_accountParams = new HashMap<String, Object>();
					Map<String, Object> tokenParams = new HashMap<String, Object>();
					Map<String, Object> recipientParams = new HashMap<String, Object>();
					
					
					bank_accountParams.put("account_number", account);
					bank_accountParams.put("routing_number", routing);
					bank_accountParams.put("country", "US");
					
					tokenParams.put("bank_account", bank_accountParams);		
					Token tokenResponse = Token.create(tokenParams);
					String token = tokenResponse.getId();
					System.out.println("Token--------------"+token);
					
					recipientParams.put("name", name);
					recipientParams.put("type","individual");
					recipientParams.put("bank_account",token);
					
					Recipient recipient = Recipient.create(recipientParams);
					String recipientId = recipient.getId();
					
					currentUser.put("RecipientId", recipientId);
					currentUser.put("bankAccountToken", token);
					
					currentUser.saveInBackground();
					
					System.out.println("Recipient--------------"+recipientId);
				} catch (AuthenticationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidRequestException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (APIConnectionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CardException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (APIException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
				return null;
			}
		
		}
	
}
